#!/bin/bash

# usage: curl -s https://git.oschina.net/haide1014/init-aliyun-ecs/raw/master/host/ChangeHostname-aliyun.sh| bash -


current_ip=$(ifconfig |grep inet |grep -v 127.0.0.1 |awk '{print $2}'|awk -F":" '{print $2}') 
echo "$current_ip"


#centos 6
######begin################################
#input hostname and ip
cat <<EOF > ./.1
eus_XXX_web01	172.30.0.X \n
EOF
#end of input
######end######################################

cat ./.1
cat .1 |grep -w "$current_ip" >.2
str1=`hostname`
echo -e "old name is $str1 \n"
str2=$(cat .2 |awk '{print $1}') #new name
#echo -e "new name is $str2 \n"
sleep 2
sed -i "s/${str1}/${str2}/g" /etc/hosts
sed -i "s/${str1}/${str2}/g" /etc/sysconfig/network

hostname $str2 && echo -e "\n### Hostname change successful! ###\n" || echo "\n###Hostname change failed! ###\n"
echo -e  "\n### hostname is `hostname`! ###\n"

# modify localtime
rm -f /etc/localtime
sed -i 's#Asia/Shanghai#America/Los_Angeles#g' /etc/sysconfig/clock
ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

#yum update
yum update -y
yum install lrzsz pigz screen -y